package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzyl.common.core.domain.entity.SysRole;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.dto.NursingTaskDto;
import com.zzyl.nursing.dto.TaskDto;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.mapper.CheckInMapper;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.mapper.NursingTaskMapper;
import com.zzyl.nursing.service.INursingTaskService;
import com.zzyl.nursing.util.IDCardUtils;
import com.zzyl.nursing.vo.NursingTaskVo;
import com.zzyl.system.mapper.SysRoleMapper;
import com.zzyl.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description NursingTaskServicelmpl
 * @Author wangyuelong
 * @Date 2025-03-02  16:30
 */
@Service
public class NursingTaskServicelmpl implements INursingTaskService {


    @Autowired
    private NursingTaskMapper nursingTaskMapper;

    @Autowired
    private ElderMapper elderMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private CheckInMapper checkInMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;



    //查询护理任务列表
    @Override
    public List<NursingTask> selectAllPage(NursingTaskDto nursingTask) {

        QueryWrapper<NursingTask> queryWrapper =new QueryWrapper<>();

        //条件
        if (nursingTask.getElderName() != null && !nursingTask.getElderName().isEmpty()) {
            queryWrapper.like("elder_name", nursingTask.getElderName());
        }

        if (nursingTask.getNurseId()!=null){
            nursingTask.getNurseId();
            queryWrapper.like("nursing_id",nursingTask.getNurseId());
        }

        if (nursingTask.getProjectId()!=null){
            queryWrapper.eq("project_id",nursingTask.getProjectId());
        }
        if (nursingTask.getStartTime() != null&& nursingTask.getEndTime() != null) {
            queryWrapper.between("estimated_server_time", nursingTask.getStartTime(), nursingTask.getEndTime());
        }
        if (nursingTask.getStatus() != null) {
            queryWrapper.eq("status", nursingTask.getStatus());
        }
        List<NursingTask> arrays = nursingTaskMapper.selectList(queryWrapper);

        for (NursingTask array : arrays) {

            String[] ids = array.getNursingId().split(",");
            List<String> names = new ArrayList<>();

            for (String id : ids) {
                long num = Long.parseLong(id);
                names.add(sysUserMapper.selectUserById(num).getNickName());
            }

            array.setNursingName(names);
        }

        return arrays;
    }

    //获取护理任务详细信息
    @Override
    public NursingTaskVo selectNursingTaskById(Long id) {
        NursingTask nursingTask = nursingTaskMapper.selectById(id);
        Elder elder =elderMapper.selectElderById(nursingTask.getElderId());
        SysRole sysRole = new SysRole();
        if (nursingTask.getUpdateBy()!=null) {
            sysRole = sysRoleMapper.selectRoleById(Long.parseLong(nursingTask.getUpdateBy()));
        }
        //vo封装
        NursingTaskVo taskVo = BeanUtil.toBean(nursingTask, NursingTaskVo.class);

        //性别
        taskVo.setSex(elder.getSex()==1? "男":"女");

        //护理员姓名
        String ids =taskVo.getNursingId();
        //System.out.println(ids);
        //编号集合
        String[] array = ids.split(",");
        List<String> names =new ArrayList<>();
        for (String name : array) {
            long num = Long.parseLong(name);
            names.add(sysUserMapper.selectUserById(num).getNickName());
        }
        taskVo.setNursingName(names);

        //年龄
        taskVo.setAge(IDCardUtils.getAgeByIdCard(elder.getIdCardNo()));

        //护理等级
        taskVo.setNursingLevelName(checkInMapper.selectCheckInById(elder.getId()).getNursingLevelName());

        //修改人
        taskVo.setUpdater(sysRole.getRoleName());



        return taskVo;
    }


    //取消任务(p)
    @Override
    public int updateNoNursingTask(TaskDto taskDto) {

        NursingTask nursingTask =new NursingTask();
        nursingTask.setCancelReason(taskDto.getReason());
        nursingTask.setId(taskDto.getTaskId());
        nursingTask.setUpdateTime(convertToDate(LocalDateTime.now()));
        nursingTask.setStatus(3);

        // nursingTask.setUpdateBy(UserThreadLocal.get().toString());

        return nursingTaskMapper.updateById(nursingTask);
        // return nursingTaskMapper.updateNoNursingTask(nursingTask);
    }

    //任务执行(p)
    @Override
    public int updateDoNursingTask(TaskDto taskDto) {
        NursingTask nursingTask =new NursingTask();

        nursingTask.setId(taskDto.getTaskId());
        nursingTask.setMark(taskDto.getMark());
        nursingTask.setTaskImage(taskDto.getTaskImage());
        nursingTask.setEstimatedServerTime(taskDto.getEstimatedServerTime());
        nursingTask.setUpdateTime(convertToDate(LocalDateTime.now()));
        nursingTask.setStatus(2);

        // nursingTask.setUpdateBy();

        return nursingTaskMapper.updateById(nursingTask);
        // return nursingTaskMapper.updateDoNursingTask(nursingTask);
    }

    //任务改期(p)
    @Override
    public int updateTimeNursingTask(TaskDto taskDto) {
        NursingTask nursingTask =new NursingTask();

        nursingTask.setId(taskDto.getTaskId());
        nursingTask.setEstimatedServerTime(taskDto.getEstimatedServerTime());
        nursingTask.setUpdateTime(convertToDate(LocalDateTime.now()));
        nursingTask.setStatus(1);

        // nursingTask.setUpdateBy();

        return nursingTaskMapper.updateById(nursingTask);
        // return nursingTaskMapper.updateTimeNursingTask(nursingTask);
    }


    //转换当前时间
    private Date convertToDate(LocalDateTime now) {
        ZonedDateTime zonedDateTime = now.atZone(ZoneId.systemDefault());
        Instant instant = zonedDateTime.toInstant();
        return Date.from(instant);
    }
}
